<?php
namespace Admin\Controller;
use Think\Controller;
class BannerController extends CommonController {
    public function liss()
    {
       $banner = D('banner')->order('id desc')->select();
       $this->assign('list',$banner);
       $this->display();
    }
       public function add()
    {
    	if(IS_POST) { 
    		$upload = new \Think\Upload();
    		$upload->maxSize   =     3145728 ;
    		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
    		$upload->savePath  =      './Public/Uploads/'; 
    		$info   =   $upload->upload();
    		if(!$info) { 
    			$this->error($upload->getError());
    		}else{
    		  	foreach($info as $file){     
    		    	$_POST['img'] = $file['savepath'].$file['savename'];    
    			}
    		}
    		$banner = D('Banner');
    		$data = $banner->create($_POST);
    		if (!$data) { 
    			$this->error($banner->getError());
    			exit;
    		} else { 
    			$num = $banner->add($data);
    			if ($num) { 
    				$this->success('添加轮播图成功！',U('Banner/liss'));
    			} else { 
    				$this->error('添加轮播图失败！');
    			}
    		}

    	} else { 
    		$this->display();
    	}
    }
    public function edit($id)
    {
        if(IS_POST) { 
            if($_FILES['img']["error"] == 0) { 
                $upload = new \Think\Upload();
                $upload->maxSize   =     3145728 ;
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
                $upload->savePath  =      './Public/Uploads/'; 
                $info   =   $upload->upload();
                if(!$info) {
                    $this->error($upload->getError());
                }else{
                    foreach($info as $file){     
                        $_POST['img'] = $file['savepath'].$file['savename'];    
                    }
                }
            }
            if(empty($_POST['status'])) { 
            	$_POST['status'] = 'off';
            } 
            $banner = D('Banner');
            $data = $banner->create($_POST);
            if (!$data) { 
                $this->error($banner->getError());
                exit;
            } else { 
                $num = $banner->where("id=$id")->save($data);
                if ($num) { 
                    $this->success('修改轮播图成功！',U('Banner/liss'));
                } else { 
                    $this->error('修改轮播图失败！');
                }
            }
        } else { 
            $banner = D('Banner')->find($id);
            $this->assign('list',$banner);
            $this->display();
        }

    }

    //删除操作
    public function del($id) 
    {
    	$result = D('Banner')->delete($id);
    	if ($result) { 
    		$this->success('轮播图删除成功！',U('Banner/liss'));
    	} else { 
    		$this->error('轮播图删除失败！');
    	}
    }

}
